package com.zl_huang.example.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zl_huang.example.dao.IScoreDaoHibernate;
import com.zl_huang.example.dao.ISubjectDaoHibernate;
import com.zl_huang.example.dao.IUserDaoHibernate;
import com.zl_huang.example.model.QueryCondition;
import com.zl_huang.example.model.Score;
import com.zl_huang.example.model.Subject;
import com.zl_huang.example.model.User;
import com.zl_huang.example.model.vo.ScoreVO;
import com.zl_huang.example.model.vo.SubjectVO;
import com.zl_huang.example.model.vo.UserVO;
@Service
public class ScoreService implements IScoreService {
	@Autowired
	private IScoreDaoHibernate  scoreDaoHibernate;
	@Autowired
	private ISubjectDaoHibernate  subjectDaoHibernate;
	@Autowired
	private IUserDaoHibernate  userDaoHibernate;

	@Override
	public Score getScoreByUserIdAndSubId(String user_id, Integer sub_id) throws Exception {
		return scoreDaoHibernate.getScoreByUserIdAndSubId(user_id, sub_id);
		
	}

	@Override
	public Score getScoreById(Integer scoreId) throws Exception {
		
		return scoreDaoHibernate.getScoreById(scoreId);
	}

	@Override
	public boolean editScore(Score score) throws Exception {
		return scoreDaoHibernate.editScore(score);		
	}

	@Override
	public List<ScoreVO> getAllScore() throws Exception {
		List<Score> scoreList =  scoreDaoHibernate.getAllScore();
		if(scoreList != null){
			List<ScoreVO> scoreVOList = new ArrayList<ScoreVO>();
			for(Score score : scoreList){
				ScoreVO scoreVO = new ScoreVO();
				BeanUtils.copyProperties(score, scoreVO);
				User user = userDaoHibernate.getUserById(score.getUser_id());
				if(user != null){
					UserVO userVO = new UserVO();
					BeanUtils.copyProperties(user, userVO);
					scoreVO.setUserVO(userVO);
				}
				Subject subject = subjectDaoHibernate.getSubjectById(score.getSubject_id());
				if(subject != null){
					SubjectVO subjectVO = new SubjectVO();
					BeanUtils.copyProperties(subject, subjectVO);
					scoreVO.setSubjectVO(subjectVO);
				}
				
				scoreVOList.add(scoreVO);
			}
			return scoreVOList;
		}else{
			return null;
		}
	}

	@Override
	public List<ScoreVO> getSelectedScore(QueryCondition queryCondition) throws Exception {
		List<Score> scoreList = scoreDaoHibernate.getSelectedScore(queryCondition);
		if(scoreList != null){
			List<ScoreVO> scoreVOList = new ArrayList<ScoreVO>();
			for(Score score : scoreList){
				ScoreVO scoreVO = new ScoreVO();
				BeanUtils.copyProperties(score, scoreVO);
				User user = userDaoHibernate.getUserById(score.getUser_id());
				if(user != null){
					UserVO userVO = new UserVO();
					BeanUtils.copyProperties(user, userVO);
					scoreVO.setUserVO(userVO);
				}
				Subject subject = subjectDaoHibernate.getSubjectById(score.getSubject_id());
				if(subject != null){
					SubjectVO subjectVO = new SubjectVO();
					BeanUtils.copyProperties(subject, subjectVO);
					scoreVO.setSubjectVO(subjectVO);
				}
				
				scoreVOList.add(scoreVO);
			}
			return scoreVOList;
		}else{
			return null;
		}	
	}

}
